<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="申请人" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入申请人ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="社团名称" prop="clubName">
        <el-input
          v-model="queryParams.clubName"
          placeholder="请输入社团名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option label="待审核" :value="0" />
          <el-option label="已通过" :value="1" />
          <el-option label="已拒绝" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="申请时间" prop="createdAt">
        <el-date-picker clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择申请时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-check"
          size="mini"
          :disabled="single"
          @click="handleApprove"
          v-hasPermi="['system:applications:approve']"
        >批量通过</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-close"
          size="mini"
          :disabled="single"
          @click="handleReject"
          v-hasPermi="['system:applications:reject']"
        >批量拒绝</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:applications:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applicationsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="申请ID" align="center" prop="applicationId" width="80" />
      <el-table-column label="申请人" align="center" prop="userName" width="100" />
      <el-table-column label="社团名称" align="center" prop="clubName" min-width="120" />
      <el-table-column label="申请理由" align="center" prop="reason" min-width="200" show-overflow-tooltip />
      <el-table-column label="申请状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <el-tag :type="getStatusType(scope.row.status)">
            {{ getStatusText(scope.row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="reviewerName" width="100" />
      <el-table-column label="审核时间" align="center" prop="reviewedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reviewedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status === 0"
            size="mini"
            type="success"
            icon="el-icon-check"
            @click="handleApprove(scope.row)"
            v-hasPermi="['system:applications:approve']"
          >通过</el-button>
          <el-button
            v-if="scope.row.status === 0"
            size="mini"
            type="danger"
            icon="el-icon-close"
            @click="handleReject(scope.row)"
            v-hasPermi="['system:applications:reject']"
          >拒绝</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['system:applications:query']"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 审核对话框 -->
    <el-dialog :title="reviewTitle" :visible.sync="reviewOpen" width="500px" append-to-body>
      <el-form ref="reviewForm" :model="reviewForm" :rules="reviewRules" label-width="80px">
        <el-form-item label="审核结果" prop="status">
          <el-radio-group v-model="reviewForm.status">
            <el-radio :label="1">通过</el-radio>
            <el-radio :label="2">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核意见" prop="reviewComment" v-if="reviewForm.status === 2">
          <el-input v-model="reviewForm.reviewComment" type="textarea" placeholder="请输入拒绝理由" :rows="3" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitReview">确 定</el-button>
        <el-button @click="cancelReview">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 申请详情对话框 -->
    <el-dialog title="申请详情" :visible.sync="viewOpen" width="600px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="申请人">{{ viewForm.userName }}</el-descriptions-item>
        <el-descriptions-item label="社团名称">{{ viewForm.clubName }}</el-descriptions-item>
        <el-descriptions-item label="申请状态">
          <el-tag :type="getStatusType(viewForm.status)">
            {{ getStatusText(viewForm.status) }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="申请时间">{{ parseTime(viewForm.createdAt) }}</el-descriptions-item>
        <el-descriptions-item label="审核人" v-if="viewForm.reviewerName">{{ viewForm.reviewerName }}</el-descriptions-item>
        <el-descriptions-item label="审核时间" v-if="viewForm.reviewedAt">{{ parseTime(viewForm.reviewedAt) }}</el-descriptions-item>
        <el-descriptions-item label="申请理由" :span="2">{{ viewForm.reason }}</el-descriptions-item>
        <el-descriptions-item label="审核意见" :span="2" v-if="viewForm.reviewComment">{{ viewForm.reviewComment }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import { listApplications, getApplications, approveApplication, rejectApplication } from "@/api/system/applications"

export default {
  name: "Applications",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 社团申请表格数据
      applicationsList: [],
      // 弹出层标题
      reviewTitle: "",
      // 是否显示审核弹出层
      reviewOpen: false,
      // 是否显示查看弹出层
      viewOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        clubId: null,
        clubName: null,
        reason: null,
        status: null,
        createdAt: null
      },
      // 审核表单参数
      reviewForm: {
        applicationIds: [],
        status: 1,
        reviewComment: ""
      },
      // 查看表单参数
      viewForm: {},
      // 审核表单校验
      reviewRules: {
        status: [
          { required: true, message: "请选择审核结果", trigger: "change" }
        ],
        reviewComment: [
          { required: true, message: "请输入拒绝理由", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询社团申请列表 */
    getList() {
      this.loading = true
      listApplications(this.queryParams).then(response => {
        this.applicationsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 获取状态类型
    getStatusType(status) {
      const statusMap = {
        0: 'warning',
        1: 'success',
        2: 'danger'
      }
      return statusMap[status] || 'info'
    },
    // 获取状态文本
    getStatusText(status) {
      const statusMap = {
        0: '待审核',
        1: '已通过',
        2: '已拒绝'
      }
      return statusMap[status] || '未知'
    },
    // 取消审核
    cancelReview() {
      this.reviewOpen = false
      this.resetReviewForm()
    },
    // 重置审核表单
    resetReviewForm() {
      this.reviewForm = {
        applicationIds: [],
        status: 1,
        reviewComment: ""
      }
      this.resetForm("reviewForm")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.applicationId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 批量通过按钮操作 */
    handleApprove(row) {
      const applicationIds = row ? [row.applicationId] : this.ids
      this.reviewForm.applicationIds = applicationIds
      this.reviewForm.status = 1
      this.reviewTitle = "审核通过"
      this.reviewOpen = true
    },
    /** 批量拒绝按钮操作 */
    handleReject(row) {
      const applicationIds = row ? [row.applicationId] : this.ids
      this.reviewForm.applicationIds = applicationIds
      this.reviewForm.status = 2
      this.reviewTitle = "审核拒绝"
      this.reviewOpen = true
    },
    /** 提交审核 */
    submitReview() {
      this.$refs["reviewForm"].validate(valid => {
        if (valid) {
          if (this.reviewForm.status === 1) {
            approveApplication(this.reviewForm).then(response => {
              this.$modal.msgSuccess("审核通过成功")
              this.reviewOpen = false
              this.getList()
            })
          } else {
            rejectApplication(this.reviewForm).then(response => {
              this.$modal.msgSuccess("审核拒绝成功")
              this.reviewOpen = false
              this.getList()
            })
          }
        }
      })
    },
    /** 查看按钮操作 */
    handleView(row) {
      getApplications(row.applicationId).then(response => {
        this.viewForm = response.data
        this.viewOpen = true
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/applications/export', {
        ...this.queryParams
      }, `applications_${new Date().getTime()}.xlsx`)
    }
  }
}
</script> 